package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.entity.College;
import com.lanou3g.ptp.entity.Major;
import com.lanou3g.ptp.organize.service.CollegeService;
import com.lanou3g.ptp.organize.service.MajorService;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.utils.PtpResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(description = "专业接口")
@RestController
public class MajorController {
    @Reference
    private MajorService majorService;
    @Reference
    private CollegeService collegeService;

    @ApiOperation("通过id获取专业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "专业id")
    })
    @GetMapping("/majors/{id}")
    public PtpResult findById(@PathVariable Long id){
        Major major = majorService.findById(id);
        College college = collegeService.findById(major.getCollegeId());
        Map<String, Object> result = new HashMap<>();
        result.put("id",major.getId());
        result.put("name",major.getName());
        result.put("collegeName",college.getName());
        result.put("leader",major.getLeader());
        return PtpResultUtils.ok(result);
    }

    @ApiOperation("通过name查找专业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "专业名称")
    })
    @GetMapping("/major/{name}")
    public PtpResult findByName(@PathVariable String name){
        Major major = majorService.findByName(name);
        return PtpResultUtils.ok(major);
    }

    @ApiOperation("添加专业")
    @PostMapping("/major")
    public PtpResult addMajor(Major major){
        majorService.addMajor(major);
        return PtpResultUtils.ok(2333);
    }

    @ApiOperation("通过id批量删除专业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "专业id")
    })
    @DeleteMapping("/major/{ids}")
    public PtpResult deleteByIds(@PathVariable Long[] ids){
        majorService.deleteByIds(ids);
        return PtpResultUtils.ok(2333);
    }

    @ApiOperation("通过id修改专业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "专业id")
    })
    @PutMapping("/major")
    public PtpResult updateById(@Param("id") Long id, @Param("major") Major major){
        majorService.updateById(id,major);
        return PtpResultUtils.ok(2333);
    }
}
